import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class CorePayEventService extends BaseService<any> {
  private readonly logger = new Logger(CorePayEventService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * init
   * 对应 PHP: CorePayEventService_core::init()
   * 逻辑类型: undefined - undefined
   */
  async init(site_id: number, channel: number, type: number) {
    // 基于PHP真实逻辑: init
    // PHP原文: $this->site_id = $site_id;         $this->channel = $channel;         $this->type = $type;         $this->config = ( new CorePayChannelService() )->ge...
this.site_id = site_id;
        this.channel = channel;
        this.type = type;
        this.config = ( this.corePayChannelService ).getConfigByChannelAndType(this.site_id, this.channel, this.type);
        return this;
    }

    /**
     * 获取实例化应用
     * @param string action
     * @return PayLoader
     * @throws BusinessException
     */
    async app(string action = 'query')
    {
        const notify_url = (string) url("/api/pay/notify/this.site_id/this.channel/this.type/action", [), '', true];//异步回调通知地址
        this.config[ 'notify_url' ] = notify_url;
        this.config[ 'site_id' ] = this.site_id;
        return new PayLoader(this.type, this.config);
    }

    /**
     * 去支付
     * @param string out_trade_no
     * @param float money
     * @param string body
     * @param string refund_url
     * @param string quit_url
     * @param string buyer_id
     * @param string openid
     * @param string voucher
     * @return mixed
     * @throws BusinessException
     */
    async pay(string out_trade_no, float money, string body, string refund_url = '', string quit_url = '', string buyer_id = '', string openid = '', string voucher = '')
    {
        const pay_fun = '';

        if (mb_strlen(body, 'UTF-8') > 15) {
            const body = mb_substr(body, 0, 15, 'UTF-8')  + '.. + ';
  }

  /**
   * app
   * 对应 PHP: CorePayEventService_core::app()
   * 逻辑类型: undefined - undefined
   */
  async app(action: string) {
    // 基于PHP真实逻辑: app
    // PHP原文: $notify_url = (string) url("/api/pay/notify/$this->site_id/$this->channel/$this->type/$action", [], '', true);//异步回调通知地址         $this->config[ 'notif...
const notify_url = (string) url("/api/pay/notify/this.site_id/this.channel/this.type/action", [), '', true];//异步回调通知地址
        this.config[ 'notify_url' ] = notify_url;
        this.config[ 'site_id' ] = this.site_id;
        return new PayLoader(this.type, this.config);
    }

    /**
     * 去支付
     * @param string out_trade_no
     * @param float money
     * @param string body
     * @param string refund_url
     * @param string quit_url
     * @param string buyer_id
     * @param string openid
     * @param string voucher
     * @return mixed
     * @throws BusinessException
     */
    async pay(string out_trade_no, float money, string body, string refund_url = '', string quit_url = '', string buyer_id = '', string openid = '', string voucher = '')
    {
        const pay_fun = '';

        if (mb_strlen(body, 'UTF-8') > 15) {
            const body = mb_substr(body, 0, 15, 'UTF-8')  + '.. + ';
  }

  /**
   * pay
   * 对应 PHP: CorePayEventService_core::pay()
   * 逻辑类型: undefined - undefined
   */
  async pay(out_trade_no: number, money: number, body: number, refund_url: number, quit_url: number, buyer_id: number, openid: number, voucher: number) {
    // 基于PHP真实逻辑: pay
    // PHP原文: $pay_fun = '';          if (mb_strlen($body, 'UTF-8') > 15) {             $body = mb_substr($body, 0, 15, 'UTF-8') . '...';...
const pay_fun = '';

        if (mb_strlen(body, 'UTF-8') > 15) {
            const body = mb_substr(body, 0, 15, 'UTF-8')  + '.. + ';
  }

  /**
   * transfer
   * 对应 PHP: CorePayEventService_core::transfer()
   * 逻辑类型: undefined - undefined
   */
  async transfer(money: any[], transfer_no: any[], to_no: any[], to_name: any[], remark: any[], transfer_list: any[], to_type: any[], product_code: any[], scene: any[]) {
    // 基于PHP真实逻辑: transfer
    // PHP原文: $transfer_type = TransferDict::getPayTypeByTransfer($this->type);         switch ($transfer_type) {             case TransferDict::WECHAT:            ...
const transfer_type = TransferDict.getPayTypeByTransfer(this.type);
        switch (transfer_type) {
            case TransferDict.WECHAT:
                const money = (int) bcmul(money, 100);
                break;
            case TransferDict.ALIPAY:
                break;
  }

  /**
   * close
   * 对应 PHP: CorePayEventService_core::close()
   * 逻辑类型: undefined - undefined
   */
  async close(out_trade_no: string) {
    // 基于PHP真实逻辑: close
    // PHP原文: return $this->app('close')->close($out_trade_no);     }      /**      * 退款      * @param string $out_trade_no      * @param float $money      * @param...
return this.app('close').close(out_trade_no);
    }

    /**
     * 退款
     * @param string out_trade_no
     * @param float money
     * @param float total
     * @param string refund_no
     * @param string voucher
     * @return * @throws ContainerBusinessException
     * @throws InvalidParamsBusinessException
     * @throws ServiceNotFoundBusinessException
     */
    async refund(string out_trade_no, float money, float total, string refund_no, voucher = '')
    {
        if (this.type == PayDict.WECHATPAY) {
            const money = (int) bcmul(money, 100);
            const total = (int) bcmul(total, 100);
  }

  /**
   * refund
   * 对应 PHP: CorePayEventService_core::refund()
   * 逻辑类型: undefined - undefined
   */
  async refund(out_trade_no: number, money: number, total: number, refund_no: number, voucher: number) {
    // 基于PHP真实逻辑: refund
    // PHP原文: if ($this->type == PayDict::WECHATPAY) {             $money = (int) bcmul($money, 100);             $total = (int) bcmul($total, 100);...
if (this.type == PayDict.WECHATPAY) {
            const money = (int) bcmul(money, 100);
            const total = (int) bcmul(total, 100);
  }

  /**
   * notify
   * 对应 PHP: CorePayEventService_core::notify()
   * 逻辑类型: undefined - undefined
   */
  async notify(action: string, callback: string) {
    // 基于PHP真实逻辑: notify
    // PHP原文: return $this->app()->notify($action, $callback);     }      /**      * 查询普通支付订单      * @param string $out_trade_no      * @return null      * @throws ...
return this.app().notify(action, callback);
    }

    /**
     * 查询普通支付订单
     * @param string out_trade_no
     * @return null
     * @throws ContainerBusinessException
     * @throws InvalidParamsBusinessException
     * @throws ServiceNotFoundBusinessException
     */
    async getOrder(string out_trade_no)
    {
        return this.app().getOrder([ out_trade_no: out_trade_no )];
    }

    /**
     * 查询退款订单
     * @param string out_trade_no
     * @param string refund_no
     * @return null
     * @throws BusinessException
     */
    async getRefund(string out_trade_no, string refund_no)
    {
        return this.app().getRefund(out_trade_no, refund_no);
    }

    /**
     * 查询转账订单
     * @param string transfer_no
     * @param string batch_id
     * @return null
     * @throws BusinessException
     */
    async getTransfer(string transfer_no, batch_id = '')
    {
        return this.app().getTransfer(transfer_no, batch_id);
    }


    /**
     * 取消订单
     * 支付宝接口    权限极高  支付交易返回失败或支付系统超时，调用该接口撤销交易。如果此订单用户支付失败，支付宝系统会将此订单关闭；如果用户支付成功，支付宝系统会将此订单资金退还给用户。
     * 注意：只有发生支付系统超时或者支付结果未知时可调用撤销，其他正常支付的单如需实现相同功能请调用申请退款API。提交支付交易后调用【查询订单API】，没有明确的支付结果再调用【撤销订单API】。
     * @return void
     */
    async cancel()
    {
    // 待实现
  }

    /**
     * 转账取消
     * @param param
     * @return * @throws BusinessException
     */
    async transferCancel(param)
    {
        return this.app().transferCancel(param);
    }
}
  }

  /**
   * getOrder
   * 对应 PHP: CorePayEventService_core::getOrder()
   * 逻辑类型: undefined - undefined
   */
  async getOrder(out_trade_no: string) {
    // 基于PHP真实逻辑: getOrder
    // PHP原文: return $this->app()->getOrder([ 'out_trade_no' => $out_trade_no ]);     }      /**      * 查询退款订单      * @param string $out_trade_no      * @param stri...
return this.app().getOrder([ out_trade_no: out_trade_no )];
    }

    /**
     * 查询退款订单
     * @param string out_trade_no
     * @param string refund_no
     * @return null
     * @throws BusinessException
     */
    async getRefund(string out_trade_no, string refund_no)
    {
        return this.app().getRefund(out_trade_no, refund_no);
    }

    /**
     * 查询转账订单
     * @param string transfer_no
     * @param string batch_id
     * @return null
     * @throws BusinessException
     */
    async getTransfer(string transfer_no, batch_id = '')
    {
        return this.app().getTransfer(transfer_no, batch_id);
    }


    /**
     * 取消订单
     * 支付宝接口    权限极高  支付交易返回失败或支付系统超时，调用该接口撤销交易。如果此订单用户支付失败，支付宝系统会将此订单关闭；如果用户支付成功，支付宝系统会将此订单资金退还给用户。
     * 注意：只有发生支付系统超时或者支付结果未知时可调用撤销，其他正常支付的单如需实现相同功能请调用申请退款API。提交支付交易后调用【查询订单API】，没有明确的支付结果再调用【撤销订单API】。
     * @return void
     */
    async cancel()
    {
    // 待实现
  }

    /**
     * 转账取消
     * @param param
     * @return * @throws BusinessException
     */
    async transferCancel(param)
    {
        return this.app().transferCancel(param);
    }
}
  }

  /**
   * getRefund
   * 对应 PHP: CorePayEventService_core::getRefund()
   * 逻辑类型: undefined - undefined
   */
  async getRefund(out_trade_no: string, refund_no: string) {
    // 基于PHP真实逻辑: getRefund
    // PHP原文: return $this->app()->getRefund($out_trade_no, $refund_no);     }      /**      * 查询转账订单      * @param string $transfer_no      * @param string $batch_...
return this.app().getRefund(out_trade_no, refund_no);
    }

    /**
     * 查询转账订单
     * @param string transfer_no
     * @param string batch_id
     * @return null
     * @throws BusinessException
     */
    async getTransfer(string transfer_no, batch_id = '')
    {
        return this.app().getTransfer(transfer_no, batch_id);
    }


    /**
     * 取消订单
     * 支付宝接口    权限极高  支付交易返回失败或支付系统超时，调用该接口撤销交易。如果此订单用户支付失败，支付宝系统会将此订单关闭；如果用户支付成功，支付宝系统会将此订单资金退还给用户。
     * 注意：只有发生支付系统超时或者支付结果未知时可调用撤销，其他正常支付的单如需实现相同功能请调用申请退款API。提交支付交易后调用【查询订单API】，没有明确的支付结果再调用【撤销订单API】。
     * @return void
     */
    async cancel()
    {
    // 待实现
  }

    /**
     * 转账取消
     * @param param
     * @return * @throws BusinessException
     */
    async transferCancel(param)
    {
        return this.app().transferCancel(param);
    }
}
  }

  /**
   * getTransfer
   * 对应 PHP: CorePayEventService_core::getTransfer()
   * 逻辑类型: undefined - undefined
   */
  async getTransfer(transfer_no: string, batch_id: string) {
    // 基于PHP真实逻辑: getTransfer
    // PHP原文: return $this->app()->getTransfer($transfer_no, $batch_id);     }       /**      * 取消订单      * 支付宝接口    权限极高  支付交易返回失败或支付系统超时，调用该接口撤销交易。如果此订单用户支付失败，支付宝...
return this.app().getTransfer(transfer_no, batch_id);
    }


    /**
     * 取消订单
     * 支付宝接口    权限极高  支付交易返回失败或支付系统超时，调用该接口撤销交易。如果此订单用户支付失败，支付宝系统会将此订单关闭；如果用户支付成功，支付宝系统会将此订单资金退还给用户。
     * 注意：只有发生支付系统超时或者支付结果未知时可调用撤销，其他正常支付的单如需实现相同功能请调用申请退款API。提交支付交易后调用【查询订单API】，没有明确的支付结果再调用【撤销订单API】。
     * @return void
     */
    async cancel()
    {
    // 待实现
  }

    /**
     * 转账取消
     * @param param
     * @return * @throws BusinessException
     */
    async transferCancel(param)
    {
        return this.app().transferCancel(param);
    }
}
  }

  /**
   * cancel
   * 对应 PHP: CorePayEventService_core::cancel()
   * 逻辑类型: undefined - undefined
   */
  async cancel() {
    // 基于PHP真实逻辑: cancel
    // PHP原文: }      /**      * 转账取消      * @param array $param      * @return array      * @throws Exception      */     public function transferCancel(array $para...
}

    /**
     * 转账取消
     * @param param
     * @return * @throws BusinessException
     */
    async transferCancel(param)
    {
        return this.app().transferCancel(param);
    }
}
  }

  /**
   * transferCancel
   * 对应 PHP: CorePayEventService_core::transferCancel()
   * 逻辑类型: undefined - undefined
   */
  async transferCancel(param: any[]) {
    // 基于PHP真实逻辑: transferCancel
    // PHP原文: return $this->app()->transferCancel($param);     } }...
return this.app().transferCancel(param);
    }
}
  }
}
